﻿using AntUnion.FrameWork.Model.Models;
using AntUnion.FrameWork.Util;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Util.Extension;

namespace AntUnion.FrameWork.Web.Areas.Console.Controllers
{
    /// <summary>
    /// 日志管理
    /// </summary>
    [Area("Console")]
    public class LogsController : BaseController
    {
        private readonly IWebHostEnvironment _hostingEnv;
        private readonly mtContext _context;
        public LogsController(mtContext context, IWebHostEnvironment hostingEnv) : base(context, hostingEnv)
        {
            _context = context;
            _hostingEnv = hostingEnv;
        }
        public IActionResult OperationLogs(int source)
        {
            ViewBag.source = source;
            return View();
        }

        public IActionResult LoginLogs(int source)
        {
            ViewBag.source = source;
            return View();
        }

        /// <summary>
        /// 获取登录日志列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<JsonResult> GetLoginLogListJson(int page, int limit, int source, int state = -1, string operationIP = "", string ipaddress = "", string firstname = "", string operationTime = "")
        {
            ListResult res = new ListResult();
            try
            {
                var data = from y in _context.LoginLogs
                           select new
                           {
                               id = y.Id,
                               contents = y.Cont,
                               userId = y.Operator,
                               firstname = y.Name,
                               source = y.Source,
                               operationIP = y.Ip,
                               ipaddress = y.Ipaddress,
                               operationBrowser = y.Browser,
                               operationSystem = y.System,
                               operationTime = y.Addtime,
                               state = y.State
                           };
                data = data.Where(k => k.source == source).OrderByDescending(k => k.operationTime);
                if (state != -1)
                {
                    data = data.Where(k => k.state == state);
                }
                if (!string.IsNullOrEmpty(operationIP))
                {
                    data = data.Where(k => k.operationIP == operationIP);
                }
                if (!string.IsNullOrEmpty(ipaddress))
                {
                    data = data.Where(k => k.ipaddress.Contains(ipaddress));
                }
                if (!string.IsNullOrEmpty(firstname))
                {
                    data = data.Where(k => k.firstname.Contains(firstname));
                }
                if (!string.IsNullOrEmpty(operationTime) && operationTime.Contains(" - "))
                {
                    string[] registrationTimes = operationTime.Split(new string[] { " - " }, StringSplitOptions.None);
                    DateTime startTime = Convert.ToDateTime(registrationTimes[0]);
                    DateTime endTime = Convert.ToDateTime(registrationTimes[1]);
                    data = data.Where(k => k.operationTime > startTime && k.operationTime < endTime);
                }
                res.Code = 0;
                res.Count = data.Count();
                if (page == 0 && limit == 0)
                {
                    res.Data = await data.ToListAsync();
                }
                else
                {
                    res.Data = await data.Skip((page - 1) * limit).Take(limit).ToListAsync();
                }
                return new JsonResult(res);
            }
            catch (Exception ex)
            {
                res.Code = 1;
                res.Msg = ex.Message;
                return new JsonResult(res);
            }
        }

        /// <summary>
        /// 获取操作日志列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<JsonResult> GetOperationLogListJson(int page, int limit, int source, int state = -1, string systemModule = "", string operationType = "", string operationIP = "", string ipaddress = "", string operators = "", string operationTime = "")
        {
            ListResult res = new ListResult();
            try
            {
                var data = from y in _context.OperationLogs
                           select new
                           {
                               id = y.Id,
                               systemModule = y.Module,
                               operationType = y.Type,
                               operators = y.Name,
                               contents = y.Cont,
                               userId = y.Operator,
                               source = y.Source,
                               operationIP = y.Ip,
                               ipaddress = y.Ipaddress,
                               operationTime = y.Addtime,
                               state = y.State
                           };
                data = data.Where(k => k.source == source).OrderByDescending(k => k.operationTime);
                if (state != -1)
                {
                    data = data.Where(k => k.state == state);
                }
                if (!string.IsNullOrEmpty(systemModule))
                {
                    data = data.Where(k => k.systemModule == systemModule);
                }
                if (!string.IsNullOrEmpty(operationType))
                {
                    data = data.Where(k => k.operationType == operationType);
                }
                if (!string.IsNullOrEmpty(operationIP))
                {
                    data = data.Where(k => k.operationIP == operationIP);
                }
                if (!string.IsNullOrEmpty(ipaddress))
                {
                    data = data.Where(k => k.ipaddress.Contains(ipaddress));
                }
                if (!string.IsNullOrEmpty(operators))
                {
                    data = data.Where(k => k.operators.Contains(operators));
                }
                if (!string.IsNullOrEmpty(operationTime) && operationTime.Contains(" - "))
                {
                    string[] registrationTimes = operationTime.Split(new string[] { " - " }, StringSplitOptions.None);
                    DateTime startTime = Convert.ToDateTime(registrationTimes[0]);
                    DateTime endTime = Convert.ToDateTime(registrationTimes[1]);
                    data = data.Where(k => k.operationTime > startTime && k.operationTime < endTime);
                }
                res.Code = 0;
                res.Count = data.Count();
                if (page == 0 && limit == 0)
                {
                    res.Data = await data.ToListAsync();
                }
                else
                {
                    res.Data = await data.Skip((page - 1) * limit).Take(limit).ToListAsync();
                }
                return new JsonResult(res);
            }
            catch (Exception ex)
            {
                res.Code = 1;
                res.Msg = ex.Message;
                return new JsonResult(res);
            }
        }

        /// <summary>
        /// 查看日志详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<IActionResult> GetLogInfo(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            OperationLog data = await _context.OperationLogs.FindAsync(id);
            if (data == null)
            {
                return NotFound();
            }
            return View(data);
        }
    }
}
